import {
  Router
} from "../../utils/common.js";
const db = wx.cloud.database();
Page({
  data: {
    userInfo: {},
    petCount: "还没有汪牌哦，点击创建",
    hasPetFriend: false,
    showPetCard: false
  },

  onLoad: function () {
    // 根据openid获取用户信息
    this.getUserInfo();
    // 根据openid获取宠物数量
    this.getPetCount();

  },
  onShow() {
    // 获取是否有汪朋狗友
    this.getPetFriend();
  },
  /**
   * 获取用户信息
   */
  getUserInfo() {
    db.collection('p_user').where({
      _openid: wx.getStorageSync("openid")
    }).get({
      success: res => {
        if (res.data.length > 0) {
          this.setData({
            userInfo: res.data[0]
          })
        } else {}
      },
      fail: err => {
        console.log('[数据库] [查询记录] 失败：');
      }
    })
  },
  /**
   * 是否有汪朋狗友
   */
  getPetFriend() {
    let that = this;
    db.collection('p_pet_collect').where({
      openId: wx.getStorageSync('openid')
    }).count({
      success: function (res) {
        let hasPetFriend = false;
        if (res.total > 0) {
          hasPetFriend = true;
        }
        that.setData({
          hasPetFriend: hasPetFriend
        });
      }
    })
  },
  /**
   * 获取汪牌数量
   */
  getPetCount() {
    const db = wx.cloud.database();
    db.collection('p_pets').where({
      _openid: wx.getStorageSync("openid")
    }).get({
      success: res => {
        if (res.data.length > 0) {
          let petCount = "您已拥有" + res.data.length + "张汪牌";
          this.setData({
            petCount: petCount,
            showPetCard: true
          })
        }
      },
      fail: err => {
        console.log('[数据库] [查询记录] 失败：');
      }
    })
  },
  navToFeedList: function () {
    Router.navigateTo("../feedlist/feedlist");
  },
  navToDogFriend: function () {
    Router.navigateTo("../dogfriend/dogfriend");
  },
  navToDogCard: function () {
    // Router.navigateTo("../greeting-card-add/greeting-card-add");
    Router.navigateTo("../share/share");
  },
  navToAbout: function () {
    Router.navigateTo("../aboutwang/aboutwang");
  },
  toWeather() {
    Router.navigateTo("../weather/weather");
  },
  navToIndex: function () {
    wx.switchTab({
      url: '../index/index'
    })
  },
  CopyLink(e) {
    wx.setClipboardData({
      data: e.currentTarget.dataset.link,
      success: res => {
        wx.showToast({
          title: '已复制',
          duration: 1000,
        })
      }
    })
  },
})